/**
 * 
 */
package org.jenkon.commons.lang.builder;

/**
 * @author jkonopka
 * 
 */
public class ToStringBuilder {

	private final StringBuilder value = new StringBuilder();

	/**
	 * @param object
	 */
	public ToStringBuilder(final Object object) {
		this.value.append(object.getClass().getSimpleName());
		this.value.append("[");
	}

	/**
	 * @param value
	 *            the value to append
	 * @return this
	 */
	public ToStringBuilder append(final Object value) {
		return append(null, value);
	}

	/**
	 * @param name
	 *            the name of the field
	 * @param value
	 *            the value of the field
	 * @return this
	 */
	public ToStringBuilder append(final String name, final Object value) {
		if (name != null) {
			this.value.append(name);
			this.value.append("=");
		}
		this.value.append(value);
		this.value.append(",");
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		final StringBuilder result = new StringBuilder(this.value.toString());
		if (result.toString().contains(",")) {
			result.deleteCharAt(result.lastIndexOf(","));
		}
		result.append("]");
		return result.toString();
	}
}
